package com.fling;

import org.robovm.apple.foundation.NSError;
import org.robovm.apple.foundation.NSObject;
import org.robovm.apple.foundation.NSURL;
import org.robovm.apple.uikit.UIAlertView;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIScreen;
import org.robovm.apple.uikit.UIViewController;
import org.robovm.apple.uikit.UIWindow;
import org.robovm.bindings.admob.GADAdSizeManager;
import org.robovm.bindings.admob.GADBannerView;
import org.robovm.bindings.admob.GADRequest;
import org.robovm.bindings.gpgs.GPGToastPlacement;
import org.robovm.bindings.gpp.GPPURLHandler;
import org.robovm.bindings.playservices.PlayServicesManager;
import org.robovm.bindings.playservices.PlayServicesManager.LoginCallback;
import org.robovm.cocoatouch.foundation.NSAutoreleasePool;

import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
import com.fling.services.GameServiceInterface;
import com.fling.services.GoogleServices;

/** Basic usage of banners and interstitials. */
public class RobovmLauncher extends IOSApplication.Delegate implements GameServiceInterface {

	private static final String BANNER_AD_UNIT_ID = "ca-app-pub-5099396459191162/3450554930";
	private static final String APP_ID = "429941587471-g6tts2556t83v868a9bqpu2kjoc2ea6j.apps.googleusercontent.com";

	private UIWindow window;
	private UIViewController rootViewController;
	private GADBannerView bannerView;
	private GADRequest request;

	private PlayServicesManager manager;
	private GoogleServices services;
	final static String LB_ID = "CgkIj_yP1MEMEAIQBg";

	@Override
	protected IOSApplication createApplication() {
		IOSApplicationConfiguration config = new IOSApplicationConfiguration();
		config.orientationLandscape = true;
		config.orientationPortrait = false;

		/* Create new ad */
		bannerView = new GADBannerView(GADAdSizeManager.banner());
		bannerView.setAdUnitID(BANNER_AD_UNIT_ID);
		rootViewController = UIApplication.getSharedApplication().getKeyWindow().getRootViewController();
		bannerView.setRootViewController(rootViewController);

		request = new GADRequest();
		bannerView.loadRequest(request);
		// We need a view controller to see ads.
		rootViewController = new UIViewController();

		// Create a standard UIWindow at screen size, add the view controller
		// and show it.
		window = new UIWindow(UIScreen.getMainScreen().getBounds());
		window.setRootViewController(rootViewController);
		window.addSubview$(rootViewController.getView());
		window.makeKeyAndVisible();

		// set up play services
		manager = new PlayServicesManager();
		manager.setClientId(APP_ID);
		manager.setViewController(rootViewController);
		manager.setToastLocation(PlayServicesManager.TOAST_BOTH, GPGToastPlacement.GPGToastPlacementTop);
		manager.setUserDataToRetrieve(true, false);
		manager.setLoginCallback(loginCallback);
		manager.didFinishLaunching();

		return new IOSApplication(new FlingHero(this, services), config);
	}

	private LoginCallback loginCallback = new LoginCallback() {

		@Override
		public void success() {
			UIAlertView uiAlert = new UIAlertView();
			uiAlert.setMessage("You are signed in to Google");
			uiAlert.show();
		}

		@Override
		public void error(NSError error) {
			// TODO Auto-generated method stub

		}
	};

	public static void main(String[] argv) {
		NSAutoreleasePool pool = new NSAutoreleasePool();
		org.robovm.cocoatouch.uikit.UIApplication.main(argv, null, RobovmLauncher.class);
		pool.drain();
	}

	// copy-paste this to your app delegate.

	public boolean openURL(UIApplication application, NSURL url, String sourceApplication, NSObject annotation) {
		return GPPURLHandler.handleURL(url, sourceApplication, annotation);
	}

	@Override
	public void logIn() {
		if (!manager.isLoggedIn()) {
			manager.login();
		}
	}

	@Override
	public void logOut() {
		manager.logout();
		UIAlertView uiAlert = new UIAlertView();
		// uiAlert.setTitle("Title");
		uiAlert.setMessage("You are signed out of Google");
		uiAlert.show();

	}

	@Override
	public boolean isSignedIn() {
		return manager.isLoggedIn();
	}

	@Override
	public void submitScore(int score) {
		if (manager.isLoggedIn()) {
			manager.postScore(LB_ID, score);
		}

	}

	@Override
	public void unlockAchievements(String achievementID) {
		if (manager.isLoggedIn()) {
			manager.unlockAchievement(achievementID);
		}

	}

	@Override
	public void showScores() {
		if (manager.isLoggedIn()) {
			manager.showLeaderboard(LB_ID);
		} else {
			UIAlertView uiAlert = new UIAlertView();
			uiAlert.setMessage("Must be signed in!");
			uiAlert.show();
		}

	}

	@Override
	public void showAchievements() {
		if (manager.isLoggedIn()) {
			manager.showAchievements();
		} else {
			UIAlertView uiAlert = new UIAlertView();
			uiAlert.setMessage("Must be signed in!");
			uiAlert.show();
		}
	}

	@Override
	public void onClick(int code) {
		// TODO Auto-generated method stub

	}
}